package com.gz.mylife.im;

import java.util.Observable;
import java.util.Observer;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.app.BaseActivity;
import com.gz.mylife.app.Plaza;
import com.gz.mylife.util.ImageUtils;
import com.gz.mylife.widget.OfflineDialog;
import com.zl.smartmall.library.util.CrashHandler;

/**
 * 私信列表
 *
 */
public class Session extends BaseActivity implements Observer{

	public static final String TAG = "Dynamic";
	
	public static final String TAB_INDEX = "tab_index";
	
	private FragmentSession mSession = new FragmentSession();
	
	private Dialog mProgressDialog;
	
//	private View mRedDot;  // 通知小红点
	
	public OfflineDialog mOfflineView;
	
	private int mTabIndex;
	private int isFromNotification;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.dynamic);
//		MessageChannel.get().register(MessageChannel.NOTIFY,this);
		MessageChannel.get().register(MessageChannel.SESSION,this);
		
//		mUserManager = new UserManager(getApplicationContext());
		
		//friend_button = (TextView)findViewById(R.id.friend_button);
		//haha_button = (TextView)findViewById(R.id.haha_button);
		//secret_sms_button = (TextView)findViewById(R.id.secret_sms_button);
		
		
		//txt_unread_haha = (TextView)findViewById(R.id.txt_unread_haha);
		//txt_unread_friend = (TextView)findViewById(R.id.txt_unread_friend);
		//txt_unread_sms = (TextView)findViewById(R.id.txt_unread_sms);
//		mRedDot = findViewById(R.id.ic_sms_notification);
		
		//friend_button.setOnClickListener(mOnClickListener);
		//haha_button.setOnClickListener(mOnClickListener);
		//secret_sms_button.setOnClickListener(mOnClickListener);
//		mNotify.setOnClickListener(mOnClickListener);
		Log.i(TAG, "Dynamic --> onCreate");
		if (savedInstanceState == null) {
			mTabIndex = getIntent().getIntExtra(TAB_INDEX, 0);
			Log.i(TAG, "Dynamic --> savedInstanceState 不为空, mTabIndex:"+mTabIndex);
			FragmentTransaction ft = getTransaction();
			ft.add(R.id.fragment_container, mSession);
			ft.commit();
		} else {
//			onItemSelected(R.id.haha_button);
			//onItemSelected(R.id.secret_sms_button);
		}
		
		mOfflineView = (OfflineDialog) getLayoutInflater().inflate(R.layout.offline_dialog, null);
		mProgressDialog = new Dialog(this, R.style.DialogTheme);
		View view = View.inflate(this, R.layout.loading_data_dialog, null);
		mProgressDialog.setContentView(view);
		mProgressDialog.setCanceledOnTouchOutside(false);
		
//		mActionBar.setTitle("聊天");
		setActionbarTitle("聊天", false);
		isFromNotification = getIntent().getIntExtra(Define.KEY_FROM_NOTIFACTION, 0);
		
		CrashHandler.d(TAG, "Dynamic oncreate");
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
//		Tools.recordBehavious(getApplicationContext(), TAG, Define.BEHAVIOUS_PAUSE);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
//		Tools.recordBehavious(getApplicationContext(), TAG, Define.BEHAVIOUS_RESUME);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
//		getMenuInflater().inflate(R.menu.dynamic, menu);
//
//		MenuItemCompat.setShowAsAction(menu.findItem(R.id.action_quick_navigate),
//				MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
//		
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			if(isFromNotification!= 0){
				Intent intent = new Intent(getApplicationContext(), Plaza.class);
				startActivity(intent);
			}
			finish();		
			return true;
		
		default:
			return super.onOptionsItemSelected(item);
		}
		
	}
	
	public FragmentTransaction getTransaction(){
		FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
//		ft.setCustomAnimations(
//				R.anim.push_left_in, 
//				R.anim.push_left_out,
//				R.anim.slide_right_in, 
//				R.anim.slide_right_out);
		return ft;
	}
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		isFromNotification = intent.getIntExtra(Define.KEY_FROM_NOTIFACTION, 0);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(mOfflineView.isShowing()){
				return mOfflineView.onKeyDown(keyCode, event);
			} 
			if(isFromNotification!= 0){
				Intent homeIntent = new Intent(getApplicationContext(), Plaza.class);
				startActivity(homeIntent);
				finish();
				return true;
			}
			finish();
		} 
		return super.onKeyDown(keyCode, event);
	}
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//MessageChannel.get().unregister(MessageChannel.NOTIFY,this);
		MessageChannel.get().unregister(MessageChannel.SESSION,this);
		ImageUtils.unbindDrawables(findViewById(R.id.layout_frame));
	}
	
	
	void onItemSelected(int id){
		Log.i(TAG, "Dynamic --> onItemSelected :"+id);
		
//		if(id == R.id.haha_button){
//			txt_unread_haha.setVisibility(View.GONE);
////			haha_button.setBackgroundResource(0);
//			
//			secret_sms_button.setTextColor(mDefaultColor);
//			//friend_button.setTextColor(mDefaultColor);
//			haha_button.setTextColor(mSelectedColor);
//			
//			haha_button.setPadding(mPaddingSize, 0, mPaddingSize, 0);
////			friend_button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
////			secret_sms_button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
//		} else if(id == R.id.friend_button){
//			PreferenceModel.putInt(PreferenceModel.unread_count_notify,0);
//			//txt_unread_friend.setVisibility(View.GONE);
////			friend_button.setBackgroundResource(R.drawable.ic_discuz_label_press);
////			secret_sms_button.setBackgroundResource(0);
//			
//			haha_button.setTextColor(mDefaultColor);
//			secret_sms_button.setTextColor(mDefaultColor);
//			//friend_button.setTextColor(mSelectedColor);
//			
//			//friend_button.setPadding(mPaddingSize, 0, mPaddingSize, 0);
////			friend_button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
////			secret_sms_button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
//		} else if(id == R.id.secret_sms_button){
//			PreferenceModel.putInt(PreferenceModel.unread_count_sms,0);
//			txt_unread_sms.setVisibility(View.GONE);
////			friend_button.setBackgroundResource(0);
////			secret_sms_button.setBackgroundResource(R.drawable.ic_discuz_label_press);
//			
//			haha_button.setTextColor(mDefaultColor);
//			//friend_button.setTextColor(mDefaultColor);
////			secret_sms_button.setTextColor(mSelectedColor);
////			secret_sms_button.setPadding(mPaddingSize, 0, mPaddingSize, 0);
////			friend_button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
////			secret_sms_button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
//		}
		
	}

	@Override
	public void update(Observable observable, Object data) {
//		Meta meta = (Meta) data;
//		if(meta.msgType == MessageChannel.SESSION){
//			if(mTabIndex != 1){  // 私信会话
//				txt_unread_sms.setVisibility(View.VISIBLE);
//			}
//		} else if(meta.msgType == MessageChannel.NOTIFY){
//			mRedDot.setVisibility(View.VISIBLE);
//		}
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}
}